package tk.heimashuma.store.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.heimashuma.store.entity.Car;
import tk.heimashuma.store.entity.Commodity;
import tk.heimashuma.store.entity.Users;
import tk.heimashuma.store.entity.dto.CarCount;
import tk.heimashuma.store.entity.dto.CarNum;
import tk.heimashuma.store.entity.dto.ClearCar;
import tk.heimashuma.store.entity.dto.State;
import tk.heimashuma.store.service.CarService;
import tk.heimashuma.store.service.CommodityService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private CarService carService;
    @GetMapping("/addCar/{id}/{num}")
    public String addCar(@PathVariable Integer id, HttpSession session, @PathVariable Integer num, Map<String,Object> map){
        Object object = session.getAttribute("user");
        Optional<Commodity> commodityOpt = commodityService.findCommodity_infoById(id);
        if(!commodityOpt.isPresent() || !Optional.ofNullable(object).isPresent()){
            return "redirect:/users/login.html";
        }
        Users user = (Users) object;
        Commodity commodity = commodityOpt.get();
        Car car = carService.saveToCar(commodity,user,num);
        car.setUser(null);
        car.setCarid(null);
        map.put("car",car);
        return "/car/add_cart_succeed";
    }
    @GetMapping("/showCar")
    public String toCar(Map<String,Object> map,HttpSession session){
        Object o = session.getAttribute("user");
        Users user = null;
        if(o instanceof  Users){
            user = (Users) o;
        }
        List<Car> car = null;
        if(Optional.ofNullable(user).isPresent()) {
            car = carService.findAllCarByUser(user);
        }
        map.put("carList",car);
        return "/car/my_car";
    }
    @PostMapping("/clearCar")
    public @ResponseBody
    ClearCar clearCar(@RequestParam(value = "ids[]")Integer [] ids, @RequestParam(value = "nums[]")Integer [] nums, HttpSession session){
        if(!Optional.ofNullable(ids).isPresent() || ids.length == 0
                || !Optional.ofNullable(nums).isPresent() || nums.length ==0){
            return null;
        }
        CarNum carNum = carService.findAllCarAndCheckNumsByIds(ids, nums);
        session.setAttribute("currentCar",carNum);
        Object o = session.getAttribute("user");
        Users user = null;
        if(Optional.ofNullable(o).isPresent()){
            user = (Users) o;
        }
        if(Optional.ofNullable(user).isPresent()){
            String t_id = carService.createOrder(carNum, user);
            ClearCar clearCar = new ClearCar();
            clearCar.setCode(200);
            clearCar.setMsg("ok");
            clearCar.setTid(t_id);
            return clearCar;
        }
        return null;
    }
    /**
     * 统计购物车个数
     */
    @GetMapping("/car_count")
    public @ResponseBody CarCount getCarCount(HttpSession session){
        Object o = session.getAttribute("user");
        Users user = null;
        if(Optional.ofNullable(o).isPresent()){
            user = (Users) o;
        }
        CarCount carCount = new CarCount();
        Long count = 0L;
        if(user != null){
             count = carService.statisticsCarByUsers(user);
        }
        carCount.setCarcount(count);
        carCount.setCode(200);
        carCount.setMsg("ok");
        return carCount;
    }
    @GetMapping("/carDel/{cid}")
    public @ResponseBody State CarDel(@PathVariable Integer cid){
        Boolean state = carService.delCar(cid);
        State s = new State();
        if(state){
            s.setCode(200);
            s.setMsg("ok");
        }else{
            s.setCode(500);
            s.setMsg("fail");
        }
        return s;
    }

}
